package com.sismul.model;

import java.util.ArrayList;
import java.util.List;

import com.sismul.MySQLiteHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BandsDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_LEVEL };

	public BandsDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Band createBand(int id, int level) {
		ContentValues values = new ContentValues();
		Band band;
		
		if ((band = get(id)) == null) { // no record
			values.put(MySQLiteHelper.COLUMN_ID, id);
			values.put(MySQLiteHelper.COLUMN_LEVEL, level);
			long insertId = database.insert(MySQLiteHelper.TABLE_BANDS, null,
					values);
			Cursor cursor = database.query(MySQLiteHelper.TABLE_BANDS,
					allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId,
					null, null, null, null);
			cursor.moveToFirst();
			Band newBand = cursorToBand(cursor);
			cursor.close();
			return newBand;
		} else {
			Log.e("level-a",""+band.getLevel());
			updateBand(id, level);
			
			Band newBand = get(band.getId());
			Log.e("level-a",""+newBand.getLevel());
		    
			return newBand;
		}

	}

	public int updateBand(Band band) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_LEVEL, band.getLevel());

		// updating row
		return database.update(MySQLiteHelper.TABLE_BANDS, values,
				MySQLiteHelper.COLUMN_ID + " = " + band.getId(), null);
	}

	public int updateBand(int id, int level) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_LEVEL, level);

		// updating row
		return database.update(MySQLiteHelper.TABLE_BANDS, values,
				MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}

	public void deleteBand(Band band) {
		long id = band.getId();
		System.out.println("Band deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_BANDS,
				MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}

	public Band get(long id) {
		List<Band> bands = new ArrayList<Band>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_BANDS,
				allColumns, MySQLiteHelper.COLUMN_ID + " = " + id + "",
				null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Band band = cursorToBand(cursor);
			bands.add(band);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return (bands.size() > 0 ? bands.get(0) : null);
	}

	public List<Band> getAllBands() {
		List<Band> bands = new ArrayList<Band>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_BANDS,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Band band = cursorToBand(cursor);
			bands.add(band);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return bands;
	}

	private Band cursorToBand(Cursor cursor) {
		Band band = new Band();
		band.setId(cursor.getLong(0));
		band.setLevel(cursor.getInt(1));
		return band;
	}
}
